package com.niit.service.impl;

import com.niit.dto.AccountDto;
import com.niit.entity.SystemAdmin;
import com.niit.form.AccountForm;
import com.niit.mapper.SystemAdminMapper;
import com.niit.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private SystemAdminMapper systemAdminMapper;

    @Override
    public AccountDto login(AccountForm accountForm) {
        AccountDto dto = new AccountDto();
        //调用systemAdminMapper的findByUsername方法找出输入账号数据库中真实的信息,和输入的账号密码进行比对
        SystemAdmin systemAdmin = this.systemAdminMapper.findByUsername(accountForm.getUsername());
        //判断用户名是否正确(用户名不存在)
        if (systemAdmin == null) {
            //用户名不存在
            dto.setCode(-1);
        } else {
            //判断密码是否正确
            if (!systemAdmin.getPassword().equals(accountForm.getPassword())) {
                //密码错误
                dto.setCode(-2);
            } else {
                //密码正确
                dto.setCode(0);
                dto.setAdmin(systemAdmin);
            }
        }
        return dto;
    }
}
